package fr.auzaiphe.admin.view;

import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.SpringLayout;

import fr.auzaiphe.admin.constantes.ConstantesAdmin;
import fr.auzaiphe.admin.model.Evenement;
import fr.auzaiphe.commun.SoundPlayer;

 /**
  * Panel de notifications
  * @author Anthony <Anthony.Rey@etu.univ-savoie.fr>
  * @date 01/05/2012
  */
public class NotificationPanel extends JPanel
{
	
	private ArrayList<NotifLabel> listeNotifications;
	private SpringLayout layout;
	private int nbNotifs;
	private int nbClicked;
	private boolean select;
	private SoundPlayer son;
	private boolean newNotifs;
	
	/**
	 * Créé un panel de notifications
	 */
	public NotificationPanel()
	{
		super();

		this.son = new SoundPlayer(ConstantesAdmin.SND_POP);
		this.layout = new SpringLayout();
		this.setLayout(this.layout);
		this.listeNotifications = new ArrayList<NotifLabel>();
		this.nbClicked = 0;
		this.select = false;
		this.newNotifs = true;
	}

	/**
	 * Ajoute un évènement au panneau de notifications
	 * @param pEvt Évènement à ajouter et afficher
	 */
	public void addBar(Evenement pEvt)
	{
		this.listeNotifications.add(new NotifLabel(pEvt));
		if(this.listeNotifications.size() > 1){
			this.layout.putConstraint(SpringLayout.NORTH, this.listeNotifications.get(this.listeNotifications.size()-2), 2, SpringLayout.SOUTH, this.listeNotifications.get(this.listeNotifications.size()-1));
		}
		this.layout.putConstraint(SpringLayout.EAST, this.listeNotifications.get(this.listeNotifications.size()-1), 0, SpringLayout.EAST, this);
		this.layout.putConstraint(SpringLayout.WEST, this.listeNotifications.get(this.listeNotifications.size()-1), 0, SpringLayout.WEST, this);
		this.add(this.listeNotifications.get(this.listeNotifications.size()-1));
		if(this.nbClicked <= this.nbNotifs)
		{
			this.listeNotifications.get(this.listeNotifications.size()-1).setColor();
		}
		this.nbNotifs++;
	}
	
	/**
	 * Supprime les notifications
	 */
	public void clearNotifications()
	{
		for(int i = 0; i< this.listeNotifications.size(); i++)
		{
			this.remove(this.listeNotifications.get(i));
			this.nbNotifs--;
		}
		this.listeNotifications.clear();
	}
	
	/**
	 * Remet à 0 le nombre de  notifications
	 */
	public void resetNbNotifs(){
		if(this.select == true)
		{
			this.nbClicked = this.nbNotifs;
			this.select = false;
			this.newNotifs = true;
		}
	}
	
	/**
	 * Récupère le nombre de notifications
	 * @return Nombre de notifications
	 */
	public int getNbNotifs(){
		return this.nbNotifs - this.nbClicked;
	}

	/**
	 * Indique que le paneau est connecté
	 */
	public void toSelect() {
		if(this.select ==  false){
			this.select = true;
		}
	}

	/**
	 *  Lance le son
	 */
	public void putSound() {
		if(this.newNotifs && this.select == false)
		{
			this.son.play();
			this.newNotifs = false;
		}
	}
	
}
